//
//  SwipableViewController.m
//  Cunli
//
//  Created by lilubin on 15/6/10.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "SwipableViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "UIColor+expanded.h"
#import "TitleBarView.h"
#import "HorizonalTableViewController.h"

@interface SwipableViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *controllers;

@end

@implementation SwipableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (instancetype)initWithTitle:(NSString *)title andSubTitles:(NSArray *)subTitles andControllers:(NSArray *)controllers
{
    return [self initWithTitle:title andSubTitles:subTitles andControllers:controllers underTabbar:NO];
}

- (instancetype)initWithTitle:(NSString *)title andSubTitles:(NSArray *)subTitles andControllers:(NSArray *)controllers underTabbar:(BOOL)underTabbar{
    
    self = [super init];
    if (self) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (title) {
            self.title = title;
        }
        
        //添加titleBar
        CGFloat titleBarHeight = 36;
        _titleBar = [[TitleBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, titleBarHeight) andTitles:subTitles];
        _titleBar.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_titleBar];
        
        //设置viewpager
        _viewPager = [[HorizonalTableViewController alloc] initWithViewControllers:controllers];
        
        CGFloat height = self.view.frame.size.height - titleBarHeight - 64 - (underTabbar ? 49 : 0);
        _viewPager.view.frame = CGRectMake(0, titleBarHeight, self.view.bounds.size.width, height);
        
        [self addChildViewController:self.viewPager];
        [self.view addSubview:_viewPager.view];
        
        __weak TitleBarView *weakTitleBar = _titleBar;
        __weak HorizonalTableViewController *weakViewPager = _viewPager;
        
        CGFloat itemWidth_line = weakTitleBar.frame.size.width * 1.0 / weakTitleBar.titleButtons.count;
        UIColor *selColor = [UIColor colorWithHexString:@"0xed5955"];
        _viewPager.changeIndex = ^(NSUInteger index){
            weakTitleBar.currentIndex = index;
            UIView *_line = [weakTitleBar viewWithTag:1050];
            [_line setFrame:CGRectMake(itemWidth_line * index + 2.0f, weakTitleBar.frame.size.height - 2.0f, itemWidth_line-4.0f, 2.0f)];
            [_line setBackgroundColor:selColor];
            for(UIButton *button in weakTitleBar.titleButtons){
                if (button.tag != index) {
                    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; //
                }else
                {
                    [button setTitleColor:selColor forState:UIControlStateNormal]; //
                    //SwipableViewController便民里设置偏移量
                    if (weakTitleBar.titleButtons.count>5) {
                        //NSLog(@"weakTitleBar:%f",weakTitleBar.frame.size.width);
                        float buttonPositn=button.frame.origin.x+button.frame.size.width;
                        if (buttonPositn>weakTitleBar.frame.size.width) {
                            [UIView animateWithDuration:0.5 animations:^{
                                [weakTitleBar setContentOffset:CGPointMake(buttonPositn-weakTitleBar.frame.size.width+button.frame.size.width*(5-button.tag), 0)];
                            }];
                        }
                        if (button.tag==0||button.tag==1) {
                            [UIView animateWithDuration:0.5 animations:^{
                                [weakTitleBar setContentOffset:CGPointMake(0, 0)];
                            }];
                        }
                    }
                }
            }
            [weakViewPager scrollToViewAtIndex:index];
        };
        
        _viewPager.scrollView = ^(CGFloat offsetRadio, NSUInteger focusIndex, NSUInteger animationIndex){
            UIButton *titleFrom = weakTitleBar.titleButtons[animationIndex];
            UIButton *titleTo = weakTitleBar.titleButtons[focusIndex];
            [titleFrom setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [titleTo setTitleColor:selColor forState:UIControlStateNormal];
            
            weakTitleBar.currentIndex = focusIndex;
            UIView *_line = [weakTitleBar viewWithTag:1050];
            CGFloat itemWidth_line = weakTitleBar.frame.size.width * 1.0 / weakTitleBar.titleButtons.count;
            [UIView animateWithDuration:0.2 animations:^{
                [_line setFrame:CGRectMake(itemWidth_line * focusIndex + 2.0f, weakTitleBar.frame.size.height - 2.0f, itemWidth_line-4.0f, 2.0f)];
            }];
            
        };
        
        _titleBar.titleButtonClicked = ^(NSInteger index){
            [weakViewPager scrollToViewAtIndex:index];
        };
#pragma - mark 为每日谈界面添加返回按钮
        if ([title isEqualToString:@"每日谈"] || [title isEqualToString:@"普法教材"] || [title isEqualToString:@"我的纠纷"]) {
            [self setNavAttributes];
        }
    }
    return self;
}

- (void)setNavAttributes
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    //    self.parentViewController.navigationItem.leftBarButtonItem = leftBarItem;
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

- (void)popSideMenu
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:NULL];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)scrollToViewAtIndex:(NSInteger)index
{
    _viewPager.changeIndex(index);
}


@end
